import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import { appContentUrl } from '../../content.url';
import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { Service } from '../../../server/server';
import { content } from '../../content.data';
import { HintsService } from '../../../public/serve/hints/hints.service';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
import { InitService } from '../../../public/serve/init/init.service';
import {appSynonymUrl} from '../../../synonym/synonym.url';
declare var $:any;
@Component({
  selector: 'app-content-product-table',
  templateUrl: './content-product-table.component.html',
  styleUrls: ['./content-product-table.component.css']
})
export class ContentProductTableComponent implements OnInit {

  total      : number = 0;
  naturalNum : number = 0;

  tabVal : Array<object>;

  isSelectAll : boolean = false;
  isChose : boolean = false;
  changeCheck:any = 0;
  projectInfo = [];
  right: any = {
    "contentProduct": {}
  };
  public taskProjectEmit: any;
  constructor(
    private server: Service,
    public request: CudTableService,
    private tableListService: CudTableService,
    private hintsService: HintsService,
    private removeTableService: RemoveTableService,
    public initService: InitService,
  ) {
    this.right = $.extend(this.right, server.getRight());
   }

  ngOnInit() {
    content.retrieval.queryCriteria = '';
    content.pagination.pageNo = 1;
    content.pagination.pageSize = 20;
    this.getContentProductList();
    this.taskProjectEmit = this.server.taskProjectEmit.subscribe((value: any) => {
      this.getContentProductList();
    });
  }

  ngOnDestroy() {
    if (this.taskProjectEmit) {
      this.taskProjectEmit.unsubscribe();
    }
  }

  /**
   * 产品内容列表
   */
  getContentProductList(){
    
    let data = this._tableData();

    if(content.retrieval.categoryId){
      data['categoryId'] = content.retrieval.categoryId;
    }
    if(content.retrieval.projectId){
      data['projectId'] = content.retrieval.projectId;
    }
    this.request.postRequest(this.server.baseUrl +appContentUrl.getProductList,data).then((res:any) => {
      if (res.result_code === '0000') {
        this.tabVal = res.result;
        this.total = res.length;
        this.naturalNum = content.pagination.naturalNum;
      }else{
        this.hintsService.messages(res.message,res.result_code);
      }
      this._cancelChecked();
      return;
    })
    .catch((err: any)=>{
      this.hintsService.messages("error","error");
      this._cancelChecked();
      return;
    });
  }

  /**
   * 全选
   * @param bool
   * @param tabVal
   */
  _selectAll(bool,tabVal){
    this.isSelectAll = this.isChose = this.removeTableService.removeAll(bool, tabVal, 'productId');
    if (this.isSelectAll == false) {
      this.getContentProductList();
    }
  }

   /**
    * 单选
    * @param bool
    * @param synonymId
    * @param length
    */
   _chose(bool, synonymId, length){
    this.isSelectAll = this.removeTableService.removeChose(bool, synonymId, length)['bool'];
   }

  /**
   * 重置表格复选框、选中的ID
   */
  private _cancelChecked(){
    this.isChose = this.isSelectAll = false;
    this.removeTableService._clearId("productId");
  }

  //查询参数
  private _tableData(){
    return {
      token         : content.retrieval.token,
      pageNo        : content.pagination.pageNo,
      pageSize      : content.pagination.pageSize,
      startTime     : content.retrieval.startTime,
      endTime       : content.retrieval.endTime,
      productType   : 0,//产品
      queryCriteria : content.retrieval.queryCriteria
    }
  }

  /**
   * 单条启用还是禁用
   */
  changeIndexingStatus(id:any,val:any){
    this.changeCheck = val;
    this.removeTableService.getRemoveTableId = [];
    this.removeTableService.keywordRemoveChose(true, id, 1)['bool'];
  }

  /**
   * 单条 启用或禁用
   */
  _review(val:any){
    this.removeTableService
    .confirmEnableDisable(content.retrieval.token,'ids',this.server.baseUrl +appContentUrl.productActiveStatus,'indexingStatus',val)
    .then(res => {
      if(res.result_code == "0000"){
        this.getContentProductList();
        this.removeTableService.getRemoveTableId = [];
      }
      this.hintsService.messages(res.message,res.result_code);
      return;
    })
    .catch(err =>{
      this.hintsService.messages('error','error');
      return;
    });
  }
}
